//
//  TJUserDataViewModel.swift
//  WeiBo
//
//  Created by TanJian on 15/12/9.
//  Copyright © 2015年 Joshpell. All rights reserved.
//

import UIKit

class TJUserDataViewModel: NSObject {

    var retweetStr:String = "转发"
    var commentStr:String = "评论"
    var attitudeStr:String = "赞"
    
    var retweetText:String?
    
    var status:TJUserData?{
        didSet{
            
        }
    }
    
    init(userData:TJUserData) {
        
        super.init()
        self.status = userData
        
        retweetStr = getString(self.status?.reposts_count ?? 0,defaulTitle: "转发")
        commentStr = getString(self.status?.comments_count ?? 0,defaulTitle: "评论")
        attitudeStr = getString(self.status?.attitudes_count ?? 0,defaulTitle: "赞")
        
        //计算转发微博内容的逻辑
        if let retweetStatus = status?.retweeted_status{
            if let name = retweetStatus.user?.name{
                retweetText = "@\(name):\(retweetStatus.text)"
            }
        }
    }
    
    private func getString(count:Int,defaulTitle:String) -> String{
        if count>0{
            if count<10000{
                return "\(count)"
            }else{
                
                let result = CGFloat(count/1000)/10
                let resultStr = "\(result)万"
                if resultStr.containsString(".0"){
                    return resultStr.stringByReplacingOccurrencesOfString(".0", withString: "")
                }
                return resultStr
            }
        }
        return defaulTitle
    }
    
    override var description:String{
        let keys = ["status"]
        return dictionaryWithValuesForKeys(keys).description
    }
    
    var headIcon:UIImage?{
    
        guard let mbrank = status?.user?.mbrank else{
            return nil
        }
        if mbrank > 0 && mbrank < 7{
            return UIImage(named: "common_icon_membership_level\(mbrank)")
        }
        return nil
    }
    
    //认证的微博等级图片
    var verifiedImage:UIImage?{
        
        guard let verified = status?.user?.verified else{
            return nil
        }
        
        //根据等级拿到不同的认证图片
        switch verified{
        case 1:
            return UIImage(named: "avatar_vip")
        case 2,3,5:
            return UIImage(named: "avatar_enterprise_vip")
        case 220:
            return UIImage(named: "avatar_grassroot")
            
        default:
            return nil
        }
    }
    
}





